package sanphone.com.runningschool.Fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.MyOrder.SendListViewAdapter;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.RequestHttp.RequestHttp;
import sanphone.com.runningschool.entity.JuShouInfo;
import sanphone.com.runningschool.entity.Order;

import static android.content.Context.MODE_PRIVATE;

public class SendMessageFragment extends Fragment {

    private List<Order> infos = null;
    private SendListViewAdapter adapter;
    private ListView send_listview;
    private SwipeRefreshLayout refreshLayout;
    private SharedPreferences sprf;
    private String id;
    private ForceOffline offline;
    private TextView empty;

    public static SendMessageFragment getInstance(){
        return new SendMessageFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.send_message_fragment,null);
        sprf=getActivity().getSharedPreferences("data",MODE_PRIVATE);
        id = sprf.getString("schoolId", null);
        offline = new ForceOffline(id,getActivity());

        send_listview=view.findViewById(R.id.send_listview);
        refreshLayout=view.findViewById(R.id.sendMessage_refresh);
        empty=view.findViewById(R.id.send_message_empty);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                refresh();
            }
        });
        //getData();
        return view;
    }

    private void getData() {
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"showBehaviorByUserId?userId="+id,getActivity());
                try {
                    String response=requestHttp.get();
                    Log.e("sendJushou", response );
                    String data=new JSONObject(response).getString("data");
                    Gson gson=new Gson();
                    infos=gson.fromJson(data,new TypeToken<List<Order>>(){}.getType());
                    if(infos.size()==0){
            empty.setVisibility(View.VISIBLE);
                    }
                    Collections.reverse(infos);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter=new SendListViewAdapter(getActivity(),infos);
                            send_listview.setAdapter(adapter);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {

                }

            }
        });
        executorService.shutdown();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
        offline.check();
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       getData();
                       offline.check();
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
}
